package dao.impl;

import dao.BaseDao;
import dao.PatientDao;
import entity.Patient;
import utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: Chapter8
 * @package: dao.impl
 * @className: PatientDaoImpl
 * @author: 翟
 * @date: 2025/3/31 11:12
 */

public class PatientDaoImpl extends BaseDao implements PatientDao {
    public PatientDaoImpl(Connection connection) {
        super(connection);
    }


    @Override
    public List<Patient> getAll() throws SQLException{
        String sql = "select * from patient";
        ResultSet resultSet = executeQuery(sql);
        List<Patient> list = new ArrayList<>();
        while (resultSet.next()) {
            Patient patient = new Patient();
            patient.setPatientId(resultSet.getInt("patientId"));
            patient.setPassword(resultSet.getString("password"));
            patient.setBirthdate(resultSet.getDate("birthdate"));
            patient.setGender(resultSet.getString("gender"));
            patient.setPatientName(resultSet.getString("patientName"));
            patient.setPhoneNum(resultSet.getString("ploneName"));
            patient.setEmail(resultSet.getString("email"));
            patient.setIdentityNum(resultSet.getString("identityNum"));
            patient.setAddress(resultSet.getString("address"));
            list.add(patient);
        }
        ConnectionUtil.closeALL(resultSet , null , null);
        return list;
    }

    @Override
    public int getPatientByIdentityNumAndPassword(String identityNum, String password) throws SQLException{
        StringBuffer stringBuffer = new StringBuffer("select count(1) from `patient` ");
        stringBuffer.append("where `identityNum` = ").append("? ");
        stringBuffer.append("and `password` = ").append("? ");
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(stringBuffer.toString() , identityNum , password);
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
            return 0;
        } finally {
            ConnectionUtil.closeALL(resultSet , null ,null);
        }
    }

    @Override
    public Patient getPatientByIdentityNumAndPassword1(String identityNum, String password) throws SQLException {
        StringBuffer stringBuffer = new StringBuffer("select * from `patient` ");
        stringBuffer.append("where `identityNum` = ").append("? ");
        stringBuffer.append("and `password` = ").append("? ");
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(stringBuffer.toString() , identityNum , password);
            if (resultSet.next()){
                Patient patient = new Patient();
                patient.setPatientId(resultSet.getInt("patientId"));
                patient.setPassword(resultSet.getString("password"));
                patient.setBirthdate(resultSet.getDate("birthdate"));
                patient.setGender(resultSet.getString("gender"));
                patient.setPatientName(resultSet.getString("patientName"));
                patient.setPhoneNum(resultSet.getString("ploneName"));
                patient.setEmail(resultSet.getString("email"));
                patient.setIdentityNum(resultSet.getString("identityNum"));
                patient.setAddress(resultSet.getString("address"));
                return patient;
            }
            return null;
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
    }
}
